package wiki.event
{
	import flash.events.Event;
	import model.Page;
	import mx.collections.ArrayCollection;

	/**
	 * An instance of this class represents a wiki related event.
	 */
	public class WikiEvent extends Event
	{
		public static const PAGE_LOADED:String = "pageLoaded";
		public static const PAGEINDEX_LOADED:String = "pageIndexLoaded";
 
		/**
		 * Constructs a new WikiEvent instance
		 * @param type - the type of this event, 
		 *               valid values: WikiEvent.PAGE_LOADED, WikiEvent.PAGEINDEX_LOADED
		 * @param payload - the payload for this event 
		 */       
 		public function WikiEvent(type:String, payload: Object=null) {
			super(type);
			_payload = payload;
			
			trace("new WikiEvent("+ type + ", " + payload);
		}

		/**
		 * If this event's type equals WikiEvent.PAGELOADED, then this function returns the Page instance representing the page 
		 * that was loaded.
		 */
		public function get page(): Page {
			if (type == PAGE_LOADED && _payload is Page) {
				return _payload as Page;
			} else {
				return null;
			}
		}
		
		/**
		 * Sets the payload for this event to the provided Page instance
		 */
		public function set page(value: Page): void {
			_payload = value;
		}
		
		/**
		 * If this event's type equals WikiEvent.PAGEINDEX_LOADED, then this function returns an ArrayCollection
		 * containing the ids of all Pages that curently exist in the wiki database.
		 */
		public function get pageIndex(): ArrayCollection {
			if (type == PAGEINDEX_LOADED && _payload is ArrayCollection) {
				return _payload as ArrayCollection;
			} else {
				return null;
			}
		}
		
		/**
		 * Sets the payload for this event to the provided collection (ArrayCollection) of page ids.
		 */
		public function set pageIndex(value: ArrayCollection): void {
			_payload = value;
		}
		
		private var _payload: Object = null;
	}
}